# 3.19 Button Control LED ## 3.19.1 Overview In this project, we control the ON/OFF of the LED via an AD button. The LED will light up if we press the button and it goes off when we press the button again. ## 3.19.2 Test Code ### 3.19.2.1 Code Flow ![6-19-2-1](./media/6-19-2-1.png) ### 3.19.2.2 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-19-tableLamp.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![control](./media/control.png), find and place ![j2](./media/j2.png) block under ![j1](./media/j1.png). ![6-1-4-1-1](./media/6-1-4-1-1.png) 3. In ![variable](./media/variable.png), find ![j7](./media/j7.png) and put it under ![j1](./media/j1.png), name the variable to “item“ and assign to “1” as its initial value. ![6-19](./media/6-19-2-2.png) 4. In ![control](./media/control.png), put ![j25](./media/j25.png) into ![j2](./media/j2.png). 5. In ![operator](./media/operator.png), find and put ![j22](./media/j22.png) into the condition box of ![j25](./media/j25.png) ![6-19](./media/6-19-2-2-3.png) 4. In ![](./media/adkey.png), find ![j8](./media/j21.png) and put it in the left part of ![j22](./media/j22.png), and set the right part to value 3500: AD key analog value > 3500 ![6-19](./media/6-19-2-2-4.png) 5. In ![control](./media/control.png), find and put block ![](./media/j16.png) into ![6-19](./media/6-19-2-2-4.png). 6. In ![operator](./media/operator.png), put ![j29](./media/j29.png) into the condition box of ![](./media/j16.png) 7. In ![variable](./media/variable.png), put ![j8](./media/j8.png) into the left part of the ![j29](./media/j29.png), and modify the right part into “1”: variable item = 1 ![6-19](./media/6-19-2-2-5.png) 8. In ![](./media/led.png), drag and put ![](./media/j3.png) in "if" of block ![6-19](./media/6-19-2-2-6.png) 9. In ![variable](./media/variable.png), put ![j8](./media/j9.png) under ![](./media/j3.png) 10. Duplicate ![](./media/j3.png) and put into "else" of block ![6-19](./media/6-19-2-2-7.png), and set LED to output “LOW” and set item variable by “1”. 11. Add a delay of 0.3s below ![](./media/j16.png). **Complete Test Code** ![6-19-2-2-8](./media/6-19-2-2-8.png) ### 3.19.2.3 Test Result After uploading code, press the red button and the red LED lights up; press it again and the LED goes off.